import logging
import os
import sys

from logging import handlers

from py_python.py_logger.log_config import log_directory, log_file_max_bytes, log_file_backup_count, logger_format

logger = logging.getLogger(__name__)


def init_logger():
    # 控制台日志
    console_handler = logging.StreamHandler(stream=sys.stdout)

    # 按大小分割日志文件
    file_handler = handlers.RotatingFileHandler(filename=os.path.join(log_directory, 'logger.log'),
                                                maxBytes=log_file_max_bytes,
                                                backupCount=log_file_backup_count)

    # 按时间分割日志文件
    # file_handler = handlers.TimedRotatingFileHandler(filename=config.log_file_name,
    #                                                  when='D', interval=10,
    #                                                  backupCount=config.log_file_backup_count)
    """
    when=''
    S
    M
    H
    D
    W
    midnight
    """
    # 日志格式
    formatter = logging.Formatter(logger_format)
    console_handler.setFormatter(fmt=formatter)
    file_handler.setFormatter(fmt=formatter)

    # 日志级别
    console_handler.setLevel(logging.DEBUG)
    file_handler.setLevel(logging.DEBUG)
    logger.setLevel(logging.DEBUG)

    # 日志对象添加处理器
    logger.addHandler(console_handler)
    logger.addHandler(file_handler)
    logger.debug('init_logger success')
    pass


if __name__ == '__main__':
    init_logger()
